package com.cnfangmao.masterdata.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.dao.EnumTypeDao;
import com.cnfangmao.masterdata.dto.EnumTypeDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.EnumType;
import com.cnfangmao.masterdata.service.EnumTypeService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 字段枚举类型 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class EnumTypeServiceImpl extends ServiceImpl<EnumTypeDao, EnumType> implements EnumTypeService {

	/* (non-Javadoc)
	 * @see com.cnfangmao.masterdata.service.EnumTypeService#getEnumTypeByCodeOrName(java.lang.String, java.lang.String)
	 */
	@Override
	public EnumType getEnumTypeByCodeOrName(String typeCode, String typeName) {
		QueryWrapper<EnumType> queryWrapper = new QueryWrapper<EnumType>();
		queryWrapper.eq("TYPE_CODE", typeCode);
		queryWrapper.or();
		queryWrapper.eq("TYPE_NAME", typeName);
		
		return getOne(queryWrapper);
	}

	/* (non-Javadoc)
	 * @see com.cnfangmao.masterdata.service.EnumTypeService#getEnumTypesByPage(com.cnfangmao.masterdata.dto.GeneralSearchDto)
	 */
	@Override
	public GeneralPageVo<EnumType> getEnumTypesByPage(GeneralSearchDto<EnumTypeDto> searchDto) {
		IPage<EnumType> page = new Page<EnumType>(searchDto.getPageNo(), searchDto.getPageSize());
		QueryWrapper<EnumType> queryWrapper = new QueryWrapper<EnumType>();
		queryWrapper.like(!StringUtils.isEmpty(searchDto.getConditions().getTypeCode()), "TYPE_CODE", searchDto.getConditions().getTypeCode());
		queryWrapper.like(!StringUtils.isEmpty(searchDto.getConditions().getTypeName()), "TYPE_NAME", searchDto.getConditions().getTypeName());
		queryWrapper.orderByDesc("MODIFIED_ON");
		
		IPage<EnumType> pageRS = page(page, queryWrapper);
		
		GeneralPageVo<EnumType> pageVo = new GeneralPageVo<EnumType>();
		pageVo.setPageNo(pageRS.getCurrent());
		pageVo.setPageSize(pageRS.getSize());
		pageVo.setTotalCount(pageRS.getTotal());
		pageVo.setRecords(pageRS.getRecords());
		
		return pageVo;
	}
	
}
